/**
 * Created by shandianhaizan on 16/6/13.
 */


'use strict';
var router = require('express').Router();
var AV = require('leanengine');

// `AV.Object.extend` 方法一定要放在全局变量，否则会造成堆栈溢出。
// 详见： https://leancloud.cn/docs/js_guide.html#对象
var Todo = AV.Object.extend('Todo');


router.get('/', function(req, res, next) {
   res.render('login',{error:""});
});


router.post('/', function(req, res, next) {
    var username = req.body.username,
        password = req.body.password;
    AV.User.logIn(username, password).then(function (loginedUser) {
        res.redirect("/backend");
    }, (function (error) {
        console.log(error);
        res.render('login',{error:error.message});
    }));
});


router.get("/auth", function(req, res, next) {
    var currentUser = AV.User.current();
     res.json({code:0, user: currentUser});
});




router.get("/logout", function(req, res, next) {
    AV.User.logOut();
    res.redirect("/login");
});





module.exports = router;
